package org.example.gykexamproject03.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Getter;
import org.example.gykexamproject03.entity.T_user;
import org.example.gykexamproject03.service.UserService;
import org.example.gykexamproject03.util.Result;
import org.example.gykexamproject03.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/exam/user")
public class UserController {
    @Autowired
    private UserService userService;

    private T_user reUser;

    /**
     * 登录接口
     *
     * @return
     */
    @PostMapping(value = "/login")
    public Result login(@RequestBody T_user user) {
        System.out.println("1111");
        System.out.println("用户登录的信息是：" + user);
        //将用户的信息提交到service层完成业务逻辑处理
        reUser = userService.login(user);
        //判断用户是否登录成功
        if (reUser != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("token", "admin-token");
            // 注意此处状态码还需要处理的，我们返回的是200，人家这里是20000
            return Result.ok(map);
        }
        return Result.build(null, ResultCodeEnum.LOGIN_MOBLE_ERROR);
    }

    /**
     * 添加用户接口
     *
     * @return
     */
    @PostMapping(value = "/addUser")
    public Result addUser(@RequestBody T_user user) {
        System.out.println("添加的用户的信息是：" + user);
        //完善添加用户的信息
        user.setLoginname(new SimpleDateFormat("yyyyMMdd").format(new Date()) + user.getSex() + new Random().nextInt(9000) + 1000);
        user.setHeadImg("sfsdfs123.jpg");
        user.setCreateTime(new Date());
        user.setType(1);
        //去完成数据库操作
        boolean save = userService.save(user);

        return save ? Result.ok() : Result.fail();

    }

    /**
     * 修改接口
     *
     * @return
     */
    @PostMapping(value = "/updateUser")
    public Result updateUser(@RequestBody T_user user) {
        System.out.println("修改的信息：" + user);
        //去完成修改操作
        boolean b = userService.updateById(user);

        return b ? Result.ok() : Result.fail();
    }

    /**
     * 获取用户信息
     */
    @GetMapping(value = "/info")
    public Result info() {
        Map<String, Object> map = new HashMap<>();
        map.put("roles", "[admin]");
        map.put("introduction", "I am a super admin");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name", reUser.getUsername());
        return Result.ok(map);

    }

    @GetMapping(value = "/delete")
    public Result delete(@RequestParam List<Long> userids){
        System.out.println("要删除的用户id是："+userids);
        //去完成批量删除
        boolean b = userService.removeBatchByIds(userids);

        return b?Result.ok():Result.fail();
    }

    @GetMapping(value = "/list")
    public Result list(int currentPage, int pageSize, String content, String sex) {
        System.out.println("当前页：" + currentPage + ",条数：" + pageSize + ",内容：" + content + ",性别：" + sex);
        //直接去分页查询
        Page<T_user> page = new Page<>(currentPage, pageSize);
        //添加查询的条件
        QueryWrapper<T_user> qw = new QueryWrapper<>();
        //下面这一段and使得content如果为空，也能使得qw有完整数据，因此能通过sex查找数据；不为空即使qw多了重复的数据，查找也不会影响
        qw.and(i -> i.like("loginname", content).or().like("username", content).or().like("tel", content).or().like("email", content));
        //qw.like("loginname", content).or().like("username", content).or().like("tel", content).or().like("email", content);
        //判断用户是否需要查询性别
        if(!sex.equals("")){
            qw.and(w -> w.eq("sex",sex));
        }
        Page<T_user> list = userService.page(page, qw);
        Map<String, Object> map = new HashMap<>();
        map.put("total", list.getTotal());
        map.put("list", list.getRecords());
        return Result.ok(map);

    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        return Result.ok();
    }

}